<?php
/**
 * Created by PhpStorm.
 * User: wcc
 * Date: 2018/11/27
 * Time: 15:08
 */
namespace think;

class Tminit{
    public function get_all_header()
    {
        // 忽略获取的header数据。这个函数后面会用到。主要是起过滤作用
        $ignore = array('host','accept','content-length','content-type');
        $headers = array();
        foreach($_SERVER as $key=>$value){
            if(substr($key, 0, 5)==='HTTP_'){
                //这里取到的都是'http_'开头的数据。
                //前去开头的前5位
                $key = substr($key, 5);
                //把$key中的'_'下划线都替换为空字符串
                $key = str_replace('_', ' ', $key);
                //再把$key中的空字符串替换成‘-’
                $key = str_replace(' ', '-', $key);
                //把$key中的所有字符转换为小写
                $key = strtolower($key);
                if(!in_array($key, $ignore)){
                    $headers[$key] = $value;
                }
            }
        }
        return $headers;
    }

    /**
     * 获取客户端IP
     */
    public function getClientIp() {
        $ip = 'unknown';
        $unknown = 'unknown';

        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
            // 使用透明代理、欺骗性代理的情况
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
            // 没有代理、使用普通匿名代理和高匿代理的情况
            $ip = $_SERVER['REMOTE_ADDR'];
        }

        // 处理多层代理的情况
        if (strpos($ip, ',') !== false) {
            // 输出第一个IP
            $ip = reset(explode(',', $ip));
        }

        return $ip;
    }

    /*
     * 判断是否加密，并且解密是否通过
    如果解密有问题则返回false，否则返回true
    */
    public function initExecute(){
        $get_all_head = $this->get_all_header();
        /* $encryption_config = [
             'is_encryption' =>true,
             'allow_ips'=>[
                 '127.0.0.1'
             ],
             'allow_domain'=>[
                 'tmadmin.com'
             ]
         ];
         file_put_contents("encryption_config.json",json_encode($encryption_config));*/
        /*if(file_exists("encryption_config.json") && strtoupper($_SERVER['REQUEST_METHOD'])!='GET'){
            $encryption_config = file_get_contents("encryption_config.json");
            $encryption_config = json_decode($encryption_config,true);
            if($encryption_config['is_encryption']){
                if(!in_array($this->getClientIp(),$encryption_config['allow_ips']) && !in_array($_SERVER['SERVER_NAME'],$encryption_config['allow_domain'])){
                    if(empty($get_all_head['tmtimestampnew']) || empty($get_all_head['tmrandomnumnew']) || empty($get_all_head['tmencryptkeynew'])){
                        return false;
                    }
                    $get_all_head['tmtimestampnew'] = (int)$get_all_head['tmtimestampnew'];
                    if(13 == strlen($get_all_head['tmtimestampnew'])){
                        if($get_all_head['tmtimestampnew']<time()*1000-5*1000 || $get_all_head['tmtimestampnew']>time()*1000+5*1000){
                            return false;
                        }
                    }
                    if(10 == strlen($get_all_head['tmtimestampnew'])){
                        if($get_all_head['tmtimestampnew']<time()-5 || $get_all_head['tmtimestampnew']>time()+5){
                            return false;
                        }
                    }
                    if($get_all_head['tmencryptkeynew'] !== md5(base64_encode(md5($get_all_head['tmtimestampnew']).$get_all_head['tmrandomnumnew']).$get_all_head['tmrandomnumnew'])){
                        return false;
                    }
                }
            }
        }*/

        if(!isset($get_all_head['tmencrypt'])){
            return true;
        }
        if(1!=$get_all_head['tmencrypt']){
            return true;
        }
        if(empty($get_all_head['tmtimestamp']) || empty($get_all_head['tmrandomnum']) || empty($get_all_head['tmencryptkey'])){
            return false;
        }
		$valid_time = 60;
		if(!empty($_FILES)){
			$valid_time = 10*60;
		}
		if(13 == strlen($get_all_head['tmtimestamp'])){
			if($get_all_head['tmtimestamp']<time()*1000-$valid_time*1000){
				return false;
			}
		}else if(10 == strlen($get_all_head['tmtimestamp'])){
			if($get_all_head['tmtimestamp']<time()-$valid_time){
				return false;
			}
		}else{
			return false;
		}
        if($get_all_head['tmencryptkey'] !== md5(base64_encode(md5($get_all_head['tmtimestamp']).$get_all_head['tmrandomnum']).$get_all_head['tmrandomnum'])){
            return false;
        }
        return true;
    }
}